<template>
	<view>
		<button type='primary' @click="getUserProfile">微信一键登录</button>
		<button type='primary' open-type="share">分享给好友</button>
	</view>
</template>
<script>
	// import {appid,appsecret} from '@/request/weinxinCommon.js'
	export default {
		data() {
			return {};
		},
		onLoad: function() {
			uni.showShareMenu({
				withShareTicket: true,
				menus: ["shareAppMessage", "shareTimeline"]
			})
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				return {
					title: '国学经典',
					path: '/pages/index/index'
				}
			}

			if (res.from === 'menu') { // 来自页面内分享按钮
				return {
					title: '国学经典',
					path: '/pages/index/index'
				}
			}

		},
		// 分享到朋友圈
		onShareTimeline() {
			return {
				title: '国学经典',
				path: '/pages/index/index',
				imageUrl: '/1.jpg'
			};
		},
		methods: {
			getUserProfile(e) {
				var that = this;
				// 获取个人信息
				uni.getUserProfile({
					desc: '用于获取用户个人信息',
					success: function(detail) {
						console.log('detail', detail)
						uni.login({
							success(res) {
								var code = res.code; //登录凭证
								uni.request({
									url: 'http://117.50.178.51:8887/api/user/v1/login',
									// 需要传给后端的数据
									data: {
										phone_data: detail.encryptedData,
										iv: detail.iv,
										code: code,
										user_info:{
											nike_name:detail.userInfo.nickName,
											gender:detail.userInfo.gender
										},
									},
									method: 'post',
									success: function(res) {
										// 将用户授权信息存储到本地
										uni.setStorageSync('userinfo', detail.userInfo)
										// 将后端返回的token存储到本地
										uni.setStorageSync('token', res.data.token)
										//通过接口获取数据库中数据
										// uni.request({
										// 	url: '',
										// 	data: {},
										// 	method: 'post',
										// 	header: {
										// 		'content-type': 'application/json'
										// 	},
										// 	success: (res) => {
										// 		that.nickName = res.data[0]
										// 			.nickName
										// 		that.avatar = res.data[0]
										// 			.avatar
										// 	}
										// })
									},
									fail: function() {
										console.log('系统错误')
									}
								})
							}
						});
					},
					fail: function() {
						uni.showModal({
							content: '取消授权将会影响相关服务，您确定取消授权吗？',
							success(res) {
								if (res.confirm) {
									wx.showToast({
										title: '已取消授权',
										duration: 1500
									})
								} else if (res.cancel) {
									this.getUserProfile()
								}
							}
						})
					}
				})
			}
		}
	};
</script>